<?php

namespace App\Admin\Controllers;

use App\Admin\Grid\NGrid;
use App\Library\Logic\OperateLogLogic;
use App\Library\Models\AdminSystemConfigModel;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid\Displayers\Actions;
use Encore\Admin\Show;

class SystemConfigController extends AdminController
{
    protected $title = '配置';

    public function grid(): NGrid
    {
        $grid = new NGrid(new AdminSystemConfigModel);
        $grid->disableFilter();
        $grid->column('id', 'ID');
        $grid->column('content', '内容')->display(function ($content) {
            return view('json', ['id' => $this->id, 'json' => $content]);
        });
        $grid->disableCreateButton();
        $grid->disableActions(false);
        $grid->actions(function (Actions $action) {
            $action->disableDelete();
            //$action->disableEdit(false);
        });
        return $grid;
    }

    /**
     * detail
     * @param int $id
     * @return Show
     */
    protected function detail(int $id): Show
    {
        $show = new Show(AdminSystemConfigModel::findOrFail($id));
        $show->field('id', 'ID');
        $show->field('content', '内容')->json();
        return $show;
    }

    public function form(): Form
    {
        $form = new Form(new AdminSystemConfigModel);
        $form->hidden('id');
        $form->ignore(['description', 'image']);
        $form->textarea('content', '内容')->required();
        /*$form->text('content->brand->海岛鬃狮','品牌');
        $form->text('content->index->box->url','盒子链接');
        $form->select('content->index->box->open','盒子开关')
            ->options(['关','开'])->default('开');
      $form->text('content->index->box->fileId','盒子图片路径');

        $form->text('content->index->pet->url','活体动物链接');
           $form->select('content->index->pet->open','活体动物开关')
               ->options(['关','开'])->default('开');
        $form->text('content->index->pet->fileId','活体动物图片路径');
        $form->select('content->index->pet->showDate','活体动物时间')
            ->options(['显示','不显示'])->default('不显示');

        $form->text('content->indexList->0->title','第1/1分类名称');
        $form->text('content->indexList->0->url','第1/1分类地址');
        $form->text('content->indexList->0->fileId','第1/1分类图片路径');

        $form->text('content->indexList->1->title','第1/2分类名称');
        $form->text('content->indexList->1->url','第1/2分类地址');
        $form->text('content->indexList->1->fileId','第1/2分类图片路径');

        $form->text('content->indexList->2->title','第1/3分类名称');
        $form->text('content->indexList->2->url','第1/3分类地址');
        $form->text('content->indexList->2->fileId','第1/3分类图片路径');

        $form->text('content->indexList->3->title','第1/4分类名称');
        $form->text('content->indexList->3->url','第1/4分类地址');
        $form->text('content->indexList->3->fileId','第1/4分类图片路径');

        $form->text('content->indexList->4->title','第1/5分类名称');
        $form->text('content->indexList->4->url','第1/5分类地址');
        $form->text('content->indexList->4->fileId','第1/5分类图片路径');

        $form->text('content->index->agent->url','代理链接');
        $form->select('content->index->agent->open','代理开关')
            ->options(['关','开'])->default('开');
        $form->text('content->index->agent->fileId','代理图片路径');

        $form->text('content->index->launch->url','启动链接');
        $form->select('content->index->launch->open','启动开关')
            ->options(['关','开'])->default('开');
        $form->text('content->index->launch->fileId','启动图片路径');

        $form->text('content->index->startTime','开始时间');
        $form->text('content->index->endTime','结束时间');

        $form->text('content->index->service->url','客服链接');
        $form->select('content->index->service->open','客服开关')
            ->options(['关','开'])->default('开');
        $form->text('content->index->service->fileId','客服图片路径');


        $form->text('content->premium','优质-premium');
        $form->text('content->withdraw->wx->max','微信提现最大值');
        $form->text('content->withdraw->wx->min','微信提现最小值');



        $form->text('content->jlTag->0->contet','第2/1分类名称');
        $form->text('content->jlTag->0->type','第2/1分类类型(text/img)')->default('img');

        $form->text('content->jlTag->1->contet','第2/2分类名称');
        $form->text('content->jlTag->1->type','第2/2分类类型(text/img)')->default('img');

        $form->text('content->jlTag->2->contet','第2/3分类名称');
        $form->text('content->jlTag->2->type','第2/3分类类型(text/img)')->default('img');

        $form->text('content->jlTag->3->contet','第2/4分类名称');
        $form->text('content->jlTag->3->type','第2/4分类类型(text/img)')->default('img');

        $form->text('content->prefectList->0->title','代理区名称');
        $form->text('content->prefectList->0->url','代理区地址');
        $form->text('content->prefectList->0->fileId','代理区图片路径');

        $form->text('content->prefectList->1->title','合作名称');
        $form->text('content->prefectList->1->url','合作地址');
        $form->text('content->prefectList->1->fileId','合作图片路径');
        $form->textarea('content->index->hot','特卖')->readonly();
        $form->textarea('content->index->activity','买1送3')->readonly();
        $form->textarea('content->index->classify','分类')->readonly();
        $form->textarea('content->index->materiel','器材专区')->readonly();
        $form->textarea('content->switch','开关专区')->readonly();
        $form->textarea('content->costArea','成本范围')->readonly();*/
        $form->html('<div>
                        <p>1.switch.index新版废弃，暂不删除!</p>
                        <p>2.activity新版废弃，暂不删除!</p>
                        <p>3.index控制首页模块，详情见图片中描述；</p>
                        <p>4.active控制满减活动的内容；</p>
                        <p>5.withdraw控制提现金额限制；</p>
                        <p>6.图片序列号使用20位数字，上传图片请到其他-上传文件中上传，上传后复制序列号回来粘贴。</p>
        </div>', '描述');
        $form->html(view('image', ['image' => fileIdToUrl('20240829160443139185')]), '图片');
        $form->saved(function (Form $form) {

            cache(['systemConfig:' . $form->id => json_decode($form->content, true)]);
        });
        $form->confirm('确定提交吗?');
        OperateLogLogic::add(Admin::user()->user_id);
        return $form;
    }
}
